﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.Entities_DAL
{
    public class LOAI_HD_DAL: BaseDAL, UTL.IBaseDAL
    {
        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            var sql = @"SELECT * FROM LOAI_HD";
            return ExecuteQuery(sql);
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            string sql = @"DELETE FROM LOAI_HD WHERE MA_LHD = '{0}'";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (LOAI_HD)obj;
            string sql = @"INSERT INTO LOAI_HD(MA_LHD, TEN_LHD) VALUES('{0}',N'{1}') ";
            sql = string.Format(sql, o.MA_LHD, o.TEN_LHD);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (LOAI_HD)obj;
            string sql = @"SET dateformat dmy; UPDATE LOAI_HD SET TEN_LHD=N'{1}' WHERE MA_LHD='{0}'";
            sql = string.Format(sql, o.MA_LHD, o.TEN_LHD);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            throw new NotImplementedException();
        }
        //tim ma so max
        public DataTable Select_max()
        {
            var sql = @"SELECT MAX(MA_LHD) AS id FROM LOAI_HD";
            return ExecuteQuery(sql);
        }
        //dem loai HD

        public string Select_count()
        {
            string sql = @"SELECT COUNT(MA_LHD) AS id FROM LOAI_HD";
            var tb = ExecuteQuery(sql);
            return tb.Rows.Count > 0 ? tb.Rows[0]["id"] + "" : "0";
        }
    }
}
